package com.example.administrator.myapplication.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.base.BaseMainActivity;
import com.example.administrator.myapplication.mode.RecordDataManager;
import com.example.administrator.myapplication.utils.AnimUtils;
import com.example.administrator.myapplication.utils.ImageUtil;
import com.example.administrator.myapplication.view.other.WebViewActivity;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.OnClick;


/**
 * Created by yang on 2017/5/3 3:17
 * EasyPermissions.PermissionCallbacks
 */
public class HomeActivity extends BaseMainActivity {
    /**
     * 扫描跳转Activity RequestCode
     */
    public static final int REQUEST_CODE = 111;
    /**
     * 请求CAMERA权限码
     */
    public static final int REQUEST_CAMERA_PERM = 101;

    /**
     * 选择系统图片Request Code
     */
    public static final int REQUEST_IMAGE = 112;


    public static void startUI(Activity activity) {
        Intent intent = new Intent(activity, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public int getContentView() {
        return R.layout.activity_home;
    }


    @Override
    public void initView() {
        super.initView();
        toolbar.setTitle("扫一扫首页");


    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public int currentItem() {
        return 0;
    }

    @OnClick({R.id.sweep, R.id.recognition, R.id.generate})
    public void onViewClicked(View view) {
        AnimUtils.shakeLeft(view, 0.85f, 6f);
        switch (view.getId()) {
            //简单模式,加载默认二维码扫描界面
            case R.id.sweep:
                Intent intent = new Intent(getApplication(), CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            //选择图片并解析
            case R.id.recognition:
                Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
                intent2.addCategory(Intent.CATEGORY_OPENABLE);
                intent2.setType("image/*");
                startActivityForResult(intent2, REQUEST_IMAGE);

                break;
//            //定制化扫描界面
//            case R.id.button3:
//                intent = new Intent(HomeActivity.this, SecondActivity.class);
//                startActivityForResult(intent, REQUEST_CODE);
//                break;

            //测试生成二维码图片
            case R.id.generate:
                Intent intent4 = new Intent(HomeActivity.this, CreateQRActivity.class);
                startActivity(intent4);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    WebViewActivity.startUI(HomeActivity.this, result);
                    RecordDataManager.getInstands().save(result);

                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(HomeActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                }
            }
        }

        /**
         * 选择系统图片并解析
         */
        else if (requestCode == REQUEST_IMAGE) {
            if (data != null) {
                Uri uri = data.getData();
                try {
                    CodeUtils.analyzeBitmap(ImageUtil.getImageAbsolutePath(this, uri), new CodeUtils.AnalyzeCallback() {
                        @Override
                        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                            Toast.makeText(HomeActivity.this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                            WebViewActivity.startUI(HomeActivity.this, result);
                            RecordDataManager.getInstands().save(result);
                        }

                        @Override
                        public void onAnalyzeFailed() {
                            Toast.makeText(HomeActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (requestCode == REQUEST_CAMERA_PERM) {
            Toast.makeText(this, "从设置页面返回...", Toast.LENGTH_SHORT)
                    .show();
        }
    }


}
